import { defineStore } from 'pinia';

export const useAuthStore = defineStore('auth', {
  state: () => ({
    token: localStorage.getItem('access_token') || '',
    isLoggedIn: !!localStorage.getItem('access_token'),
    tokenExpiresAt: localStorage.getItem('token_expires_at') || ''
  }),

  actions: {
    setToken(token: string, expiresAt: number) {
      this.token = token;
      this.isLoggedIn = true;
      this.tokenExpiresAt = expiresAt.toString();
      localStorage.setItem('access_token', token);
      localStorage.setItem('token_expires_at', expiresAt.toString());
    },

    clearToken() {
      this.token = '';
      this.isLoggedIn = false;
      this.tokenExpiresAt = '';
      localStorage.removeItem('access_token');
      localStorage.removeItem('token_expires_at');
    },
    logout() {
      this.clearToken();
    }
  }
});